package com.tomecode.tools.maven.settings.gui.frm;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.grouplayout.GroupLayout;
import org.eclipse.swt.layout.grouplayout.LayoutStyle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.tomecode.tools.maven.settings.gui.images.ImageFactory;
import com.tomecode.tools.maven.settings.gui.utils.SWTResourceManager;

/**
 * Dialog for editing nonProxyHost
 * 
 * @author Tomas Frastia
 * @see http://www.tomecode.com or
 *      http://code.google.com/p/maven-settings-editor/
 * 
 */
public final class FrmNonProxyHost extends Dialog {

	private String newProxyHost;
	private Shell shlNonproxyhosts;
	private Text textNonProxyHost;
	private List<String> listOfNonProxyHosts;
	private String exitsNonProxyHost;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public FrmNonProxyHost(Shell parent, List<String> listOfNonProxyHosts) {
		super(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		setText("New Non Proxy Host");
		this.listOfNonProxyHosts = listOfNonProxyHosts;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public final String open(String nonProxyHost) {
		createContents(nonProxyHost);
		shlNonproxyhosts.open();
		shlNonproxyhosts.layout();
		Display display = getParent().getDisplay();
		while (!shlNonproxyhosts.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return newProxyHost;
	}

	/**
	 * Create contents of the dialog.
	 */
	private final void createContents(String nonProxyHost) {
		shlNonproxyhosts = new Shell(getParent(), getStyle());
		shlNonproxyhosts.setImage(ImageFactory.MAVEN_SETTINGS_EDITOR_16);
		shlNonproxyhosts.setSize(657, 166);

		if (nonProxyHost == null) {
			shlNonproxyhosts.setText("New Non Proxy Hosts");
			nonProxyHost = "";
		} else {
			shlNonproxyhosts.setText("Edit Non Proxy Hosts");
		}
		Composite composite = new Composite(shlNonproxyhosts, SWT.NONE);
		final Button btnSave = new Button(composite, SWT.NONE);

		Group grpCancel = new Group(composite, SWT.NONE);
		final Label labelWarning = new Label(grpCancel, SWT.NONE);
		Label lblNonProxyHost = new Label(grpCancel, SWT.NONE);
		lblNonProxyHost.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblNonProxyHost.setBounds(10, 29, 114, 14);
		lblNonProxyHost.setText("Non Proxy Host");

		textNonProxyHost = new Text(grpCancel, SWT.BORDER);
		textNonProxyHost.addKeyListener(new KeyAdapter() {
			@Override
			public final void keyReleased(KeyEvent e) {
				String result = textNonProxyHost.getText();
				if (result.trim().length() == 0) {
					labelWarning.setText("Non Proxy Host can't be empty");
					btnSave.setEnabled(false);
				} else {
					if (existsNonProxyHosts(result)) {
						labelWarning.setText("Non Proxy Host already exists");
						btnSave.setEnabled(false);
					} else {
						labelWarning.setText("");
						btnSave.setEnabled(true);
					}
				}
			}
		});
		textNonProxyHost.setText(nonProxyHost);
		textNonProxyHost.setBounds(130, 26, 511, 19);

		labelWarning.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		labelWarning.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD | SWT.ITALIC));
		labelWarning.setBounds(10, 51, 623, 14);

		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				newProxyHost = textNonProxyHost.getText().trim();
				shlNonproxyhosts.close();
			}
		});
		btnSave.setText("Save");

		Button btnCancel = new Button(composite, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				shlNonproxyhosts.close();
			}
		});
		btnCancel.setText("Cancel");
		GroupLayout gl_composite = new GroupLayout(composite);
		gl_composite.setHorizontalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(grpCancel, GroupLayout.DEFAULT_SIZE, 651, Short.MAX_VALUE)
				.add(gl_composite.createSequentialGroup().add(449).add(btnSave, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED).add(btnCancel, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addContainerGap()));
		gl_composite.setVerticalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(
				gl_composite.createSequentialGroup().add(grpCancel, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.UNRELATED)
						.add(gl_composite.createParallelGroup(GroupLayout.BASELINE).add(btnCancel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE).add(btnSave, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)).addContainerGap()));
		composite.setLayout(gl_composite);
		GroupLayout gl_shlNonproxyhosts = new GroupLayout(shlNonproxyhosts);
		gl_shlNonproxyhosts.setHorizontalGroup(gl_shlNonproxyhosts.createParallelGroup(GroupLayout.LEADING).add(composite));
		gl_shlNonproxyhosts.setVerticalGroup(gl_shlNonproxyhosts.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE));
		shlNonproxyhosts.setLayout(gl_shlNonproxyhosts);

	}

	private final boolean existsNonProxyHosts(String result) {
		for (String host : listOfNonProxyHosts) {
			if (exitsNonProxyHost != null) {
				if (!exitsNonProxyHost.trim().equalsIgnoreCase(host.trim())) {
					if (result.trim().equalsIgnoreCase(host.trim())) {
						return true;
					}
				}
			} else {
				if (result.trim().equalsIgnoreCase(host.trim())) {
					return true;
				}
			}

		}
		return false;
	}
}
